﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework.Constraints;
using NUnit.Framework;
using NHibernate;
using SAKS.Models;

namespace SAKS.UnitTest.CacheTest
{
    [TestFixture]
    public class NHibernateCacheTest : TestBase
    {

        [Test]
        public void SecondLevelCacheReadTest()
        {
            var role = new Role
            {
                RoleName = "CacheReadTest",
            };
            Guid id;
            using (var session = sessionFactory.OpenSession())
            {
                id = (Guid)session.Save(role);
                Console.WriteLine("这里会执行一条Insert语句");
                session.Flush();
                Console.WriteLine("由于有一级缓存，这里不会执行Select语句");
                var role1 = session.Get<Role>(id);
            }
            using (var session = sessionFactory.OpenSession())
            {
                Console.WriteLine("由于有二级缓存，这里也不会执行Select语句");
                var role2 = session.Get<Role>(id);
            }
        }


        //NHibernate中，Get,Load方法才会建立缓存，Save，SaveOrUpdate不会
        //这里设置的是read-write缓存，所以可以更新
        [Test]
        public void SecondLevelCacheWriteTest()
        {
            var role = new Role
            {
                RoleName = "CacheWriteTest",
                Description = "Original"
            };
            Guid id;
            using (var session = sessionFactory.OpenSession())
            {
                Console.WriteLine("插入数据");
                id = (Guid)session.Save(role);
                session.Flush();
            }
            
            using (var session = sessionFactory.OpenSession())
            {
                Console.WriteLine("由于二级缓存还没有建立，这里会执行Select语句");
                var role3 = session.Get<Role>(id);
            }

            using (var session = sessionFactory.OpenSession())
            {
                Console.WriteLine("由于二级缓存已经建立，这里不会执行Select语句");
                var role1 = session.Get<Role>(id);
                Console.WriteLine("开始更新");
                role1.Description = "Changed";
                session.Update(role1);
                session.Flush();
            }

            using (var session = sessionFactory.OpenSession())
            {
                Console.WriteLine("由于缓存已经失效，这里会执行Select语句");
                var role4 = session.Get<Role>(id);
                Assert.AreEqual("Changed", role4.Description);
            }
        }

        [Test]
        public void QueryCacheTest()
        {
            User user = new User
            {
                IsAnonymous = false,
                LastActivityDate = DateTime.Now,
                UserName = "QueryCacheTest",
                LoweredUserName = "querycachetest",
            };
            using (var session = sessionFactory.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    session.Save(user);
                    session.Flush();

                    Console.WriteLine("第一次查询某数据，显式缓存查询结果");
                    IList<User> users =
                        session.QueryOver<User>().Where(u => u.UserName == "QueryCacheTest").Cacheable().CacheRegion("User")
                        .List<User>();
                    tx.Commit();
                    Assert.AreEqual(1, users.Count);
                }
            }

            using (var session = sessionFactory.OpenSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    Console.WriteLine("第二次查询某数据，显式缓存查询结果");
                    IList<User> users =
                        session.QueryOver<User>().Where(u => u.UserName == "QueryCacheTest").Cacheable().CacheRegion("User")
                        .List<User>();
                    tx.Commit();
                    Assert.AreEqual(1, users.Count);
                }
            }
        }

    }
}
